package top.kaoshanji.p2ch16a;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 代码清单-p2ch16a-475页a：可重入锁ReentrantLock的基本用法
 * author kaoshanji
 * ^_^
 * create time 2025/2/26 16:40
 */
public class Counter {

    // 作为实例属性存在
    private final Lock lock = new ReentrantLock();
    private volatile int count;

    public void incr() {
        // 标准写法
        lock.lock();
        try {
            count++;
        } finally {
            lock.unlock();
        }
    }

    public int getCount() {
        return count;
    }

}
